﻿using System;
using System.Collections.Generic;
using System.Linq;
using NH = NHibernate;
using NHibernate.Linq;


namespace MVCQuick.Framework.Repository.NHibernate
{
    /// <summary>
    /// 
    /// </summary>
    public class NHibernateRepository : IRepository
    {
        private NH.ISession session;
        private bool useNHibernateManager = true;

        /// <summary>
        /// 
        /// </summary>
        public NHibernateRepository()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="session"></param>
        public NHibernateRepository(NH.ISession session)
        {
            this.session = session;
            this.useNHibernateManager = false;
        }


        /// <summary>
        /// 
        /// </summary>
        public NH.ISession Session
        {
            get
            {
                if (!useNHibernateManager)
                {
                    return session;
                }
                else
                {
                    return NHibernateManager.GetCurrentSession();
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        public void Save<TEntity>(TEntity entity) where TEntity : EntityBase
        {
            NH.ISession session = Session;

            session.Save(entity);
            session.Flush();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        public void Update<TEntity>(TEntity entity) where TEntity : EntityBase
        {
            NH.ISession session = Session;

            session.Update(entity);
            session.Flush();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        public void SaveOrUpdate<TEntity>(TEntity entity) where TEntity : EntityBase
        {
            NH.ISession session = Session;

            session.SaveOrUpdate(entity);
            session.Flush();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        public void Delete<TEntity>(TEntity entity) where TEntity : EntityBase
        {
            NH.ISession session = Session;

            session.Delete(entity);
            session.Flush();
        }
       
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public TEntity Get<TEntity>(int id) where TEntity : EntityBase
        {
            NH.ISession session = Session;

            return session.Get<TEntity>(id);
        }       

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <returns></returns>
        public IQueryable<TEntity> Query<TEntity>()
        {

            NH.ISession session = Session;

            return session.Query<TEntity>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ITransaction BeginTransaction()
        {
            NH.ISession session = Session;

            return new NHibernateTransaction(session);
        }

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            if (this.session != null)
            {
                this.session.Flush();
                CloseSession();
            }
        }   


        private void CloseSession()
        {
            session.Close();
            session.Dispose();
            session = null;
        }
    }
}